package com.dk.controller;

import com.dk.Vo.ArticleVo;
import com.dk.pojo.ArtType;
import com.dk.pojo.Article;
import com.dk.pojo.UUser;
import com.dk.service.ArtTypeServiceI;
import com.dk.service.ArticleServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("articleController")
public class ArticleController {

    //文章
    @Autowired
    private ArticleServiceI articleServiceI;

    //文章类型
    @Autowired
    private ArtTypeServiceI artTypeServiceI;

    //跳转文章查询页面
    @RequestMapping("list")
    public String listGet(){
        return "article/list";
    }

    //查询文章
    @RequestMapping("getInfoServer")
    @ResponseBody
    public List<ArticleVo> getInfoServer(HttpServletRequest request){

        //获取登录用户的信息
        UUser uUser = (UUser)request.getSession().getAttribute("tUser");
        String uname = uUser.getUname();
        List<ArticleVo> list = articleServiceI.getInfoServer(uname);
        return list;
    }

    //跳转增加文章
    @RequestMapping("insertInfo")
    public String insertInfo(Model model){

        //文章类型查询
        List<ArtType> artTypes = artTypeServiceI.getInfoServer();
        model.addAttribute("artTypes",artTypes);

        return "article/add";
    }

    //确认增加文章
    @RequestMapping("saveById")
    @ResponseBody
    public int saveById(Article article, Model model, HttpServletRequest request){

        //获取登录用户的信息
        UUser uUser = (UUser)request.getSession().getAttribute("tUser");
        articleServiceI.saveById(article,uUser);
        return 1;
    }

    //文章修改回显
    @RequestMapping("updateInfo/{artId}")
    public String updateInfo(@PathVariable("artId") Integer artId,Model model){
        //文章id 回显
         Article article = articleServiceI.getUpdateById(artId);
        model.addAttribute("article",article);

        //文章类型查询
        List<ArtType> artTypes = artTypeServiceI.getInfoServer();
        model.addAttribute("artTypes",artTypes);

        return "article/update";
    }

    //确认修改文章
    @RequestMapping("updateById")
    @ResponseBody
    public int updateById(Article article){
        articleServiceI.updateById(article);
        return 1;
    }

    //删除文章
    @RequestMapping("deleteById/{artId}")
    @ResponseBody
    public int deleteById(@PathVariable("artId") Integer artId){
        articleServiceI.deleteById(artId);
        return 1;
    }

    //文章审核
    @RequestMapping("listGet")
    public String getList(){
        return "article/lists";
    }
    //文章审核查询
    @RequestMapping("getInfoServers")
    @ResponseBody
    public List<ArticleVo> getInfoServers(){
        List<ArticleVo> list = articleServiceI.getInfoServers();
        return list;
    }

    //审核通过
    @RequestMapping("updateByIdd/{artId}")
    @ResponseBody
    public int updateByIdd(@PathVariable("artId")Integer artId){

        //查询进行修改
        Article article = articleServiceI.getUpdateInfo(artId);
        article.setExamine(1);

        //确定修改
        articleServiceI.updateByIdd(article);
        return 1;
    }

    //驳回
    @RequestMapping("deleteInfo/{artId}")
    @ResponseBody
    public int deleteInfo(@PathVariable("artId")Integer artId){

        //查询进行修改
        Article article = articleServiceI.getUpdateInfo(artId);
        article.setExamine(2);

        //确定修改
        articleServiceI.updateByIdd(article);
        return 1;
    }
}
